package com.ciquan.server.controller;

import com.ciquan.server.bean.*;
import com.ciquan.server.service.*;
import com.ciquan.server.util.web.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by kakalee on 15/3/27.
 */
@Controller
@RequestMapping("/api/advertisement/")
public class ServiceAdvertisementController extends AbstractController {

    @Resource
    private AdvertisementService advertisementService;

    @Resource
    private WorksRecommendService worksRecommendService;

    @Resource
    private ArtistRecommendService artistRecommendService;

    @Resource
    private UserRecommendService userRecommendService;

    @Resource
    private KeyValueService keyValueService;

    @RequestMapping(value = "/list", produces = "text/plain;charset=UTF-8", method = {RequestMethod.POST})
    @ResponseBody
    public String list() {
        Result result = new Result();
        List<Advertisement> allAdvertisements = advertisementService.selectList();
        List<WorksRecommend> worksRecommends = worksRecommendService.selectList();
        List<ArtistRecommend> artistRecommends = artistRecommendService.selectList();
        List<UserRecommend> userRecommends = userRecommendService.selectList();

        List<Advertisement> advertisements = new ArrayList<Advertisement>();
        for(Advertisement advertisement :allAdvertisements) {
            if(advertisement.getType() == 0) {
                advertisements.add(advertisement);
            }
        }

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("advertisements", advertisements);
        map.put("worksRecommends", worksRecommends);
        map.put("artistRecommends", artistRecommends);
        map.put("userRecommends", userRecommends);
        result.setSuccess(true);
        result.setData(map);
        result.setStatusmsg("获取信息成功");
        return result.toString();
    }

    @RequestMapping(value = "/start", produces = "text/plain;charset=UTF-8", method = {RequestMethod.POST})
    @ResponseBody
    public String start() {
        Result result = new Result();
        Map<String, Object> map = new HashMap<String, Object>();
        List<Advertisement> allAdvertisements = advertisementService.selectList();
        for(Advertisement advertisement :allAdvertisements) {
            if(advertisement.getType() == 1) {
                map.put("advertisement", advertisement);
                break;
            }
        }
        result.setSuccess(true);
        result.setData(map);
        result.setStatusmsg("获取信息成功");
        return result.toString();
    }

    @RequestMapping(value = "/keyWords", produces = "text/plain;charset=UTF-8", method = {RequestMethod.POST})
    @ResponseBody
    public String keyWords() {
        Result result = new Result();
        KeyValue keyValue = new KeyValue();
        keyValue.setTitle("keywords");
        keyValue = keyValueService.selectOne(keyValue);
        String keyWords[] = keyValue.getContent().split(",");
        result.setSuccess(true);
        result.setData(Arrays.asList(keyWords));
        result.setStatusmsg("获取关键词成功");
        return result.toString();
    }

    @RequestMapping(value = "/version", produces = "text/plain;charset=UTF-8", method = {RequestMethod.POST})
    @ResponseBody
    public String version(HttpServletRequest request) {
        Result result = new Result();
        result.setSuccess(false);
        String version = ServletRequestUtils.getStringParameter(request, "version", "");

        KeyValue keyValue = new KeyValue();
        keyValue.setTitle("version");
        keyValue = keyValueService.selectOne(keyValue);
        if(keyValue != null) {
            String[] values = keyValue.getContent().split(",");
            String localVersion = values[0];
            if(localVersion.compareTo(version) > 0) {
                result.setSuccess(true);
                result.setData(values[1]);
            }
        }
        return result.toString();
    }
}
